<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP starting page</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext.onReady(function() {
	
	var window = "null";
	var a_username = "null";
	var a_password = "null";
	var a_url = "null";
	var a_driver = "null";
	var a_tablename = "null";
	var a_schemaname = "null";
	
	
	
	
	
	var proxy_1 = new Ext.data.HttpProxy({
			url : 'scriptquery.svt?kind=showtables'
		});

		var reader_1 = new Ext.data.JsonReader({}, [{
			name : 'tablename'
		}, {
			name : 'hivetablename'
		}, {
			name : 'hdfspath'
		}]);

		var store_1 = new Ext.data.Store({
			proxy : proxy_1,
			reader : reader_1
		});

		store_1.load();

		var tables_ = new Ext.grid.GridPanel({
			store : store_1,
			margins : '5 0 5 5',
			title : 'Hive Tables',
			frame : true,
			width : 400,
			region : 'west',
			columns : [{
				id : 'tablename',
				header : 'Table Name',
				width : 150,
				sortable : true,
				dataIndex : 'tablename',
				align : 'center'
			}, {
				id : 'hivetablename',
				header : 'Hive Table Name',
				width : 200,
				sortable : true,
				dataIndex : 'hivetablename',
				align : 'center'
			}, {
				id : 'hdfspath',
				header : 'HDFS PATH',
				width : 200,
				sortable : true,
				dataIndex : 'hdfspath',
				align : 'center'
			}]
		});
	
	
	
	
	
	

		var proxy_4 = new Ext.data.HttpProxy({
			url : 'datamigration.svt?kind=migrating'
		});

		var reader_4 = new Ext.data.JsonReader({}, [{
			name : 'tablename'
		}, {
			name : 'driver'
		}, {
			name : 'url'
		}, {
			name : 'username'
		}, {
			name : 'password'
		}, {
			name : 'progress'
		}]);

		var store_4 = new Ext.data.Store({
			proxy : proxy_4,
			reader : reader_4
		});

		store_4.load();

		var tables = new Ext.grid.GridPanel({
			store : store_4,
			region : 'west',
			frame : true,
			width : 400,
			title : '迁移任务',
			margins : '5 5 5 0',
			tbar:[	new Ext.Button(
											{
												text : '表详情',
												width : 70,
												handler : function() {
													if (window == "null") {
														Ext.MessageBox
																.alert(
																		"Prompt",
																		"Please choose a data source.");
													}
													window.show();
													window = "null";
												}
											})],
			columns : [ {
				id : 'Table Name',
				width : 150,
				header : '数据库表明',
				sortable : true,
				dataIndex : 'tablename',
				align : 'center'
			}, {
				id : 'Driver',
				header : '驱动',
				width : 150,
				sortable : true,
				dataIndex : 'driver',
				align : 'center'
			}, {
				id : 'Database',
				header : '数据库实例',
				sortable : true,
				width : 220,
				dataIndex : 'url',
				align : 'center'
			}, {
				id : 'User',
				header : '用户',
				sortable : true,
				dataIndex : 'username',
				align : 'center'
			}, {
				id : 'Password',
				header : '密码',
				sortable : true,
				dataIndex : 'password',
				align : 'center'
			},{
				id : 'Progress',
				header : 'Progress',
				sortable : true,
				dataIndex : 'progress',
				align : 'center'
			}]

		});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////BUTTONS
var queryPattern = new Ext.form.ComboBox({
			　	triggerAction:'all',
　				mode:'local',
				id:'queryPattern',//////////////////////////////////////////////////////////// vender
				fieldLabel : '查询方式',
				name : 'name',
				xtype : 'combo',
				width:200,
				store : new Ext.data.SimpleStore({
					fields : ['text', 'value'],
					data : [['多表','2'],
							['单表','1']
							]
				// from states.js  
				}),
				valueField:'value',
　				displayField:'text'
			});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////comboxbox

// var c_store = new Ext.data.Store({  
//         proxy: new Ext.data.HttpProxy({  
//             url: 'datamigration.svt?kind=column&username='
// 								+ a_username + '&url=' + a_url + '&password='
// 								+ a_password + '&driver=' + a_driver
// 								+ '&schemaname=' +a_schemaname + '&tablename='
// 								+ a_tablename 
//         }),  
//         reader: new Ext.data.JsonReader({},['columname','columname']),  
//         remoteSort: false  
//      });  
//      c_store.load();  
//      var j_t_store = new Ext.data.Store({  
//         proxy: new Ext.data.HttpProxy({  
//             url: 'province.do'  
//         }),  
//         reader: new Ext.data.JsonReader({},['table','tablename']),  
//         remoteSort: false  
//      });  
//      j_t_store.load();  
//         var j_f_store = new Ext.data.Store({  
//         proxy: new Ext.data.HttpProxy({  
//             url: 'province.do'  
//         }),  
//         reader: new Ext.data.JsonReader({},['jfield','jfieldname']),  
//         remoteSort: false  
//      });  
//      j_f_store.load(); 
			
// 			var choosefields = new Ext.form.ComboBox({
// 				fieldLabel:'Choose Field',
// 				store:c_store,
// 				width:200
// 			});
			
			var joinPattern = new Ext.form.ComboBox({
			　	triggerAction:'all',
　				mode:'local',
				id:'joinpattern',//////////////////////////////////////////////////////////// vender
				fieldLabel : 'Join Pattern',
				name : 'joinpattern',
				xtype : 'combo',
				width:200,
				store : new Ext.data.SimpleStore({
					fields : ['text', 'value'],
					data : [['No join','1'],
							['Left outter join','2'],
							['Right outter join','3'],
							['Just join','4']
							]
				// from states.js  
				}),
				valueField:'value',
　				displayField:'text'
			});
			
// 			var joinTable = new Ext.form.ComboBox({
// 				fieldLabel:'Join Table',
// 				width:200,
// 				id:'jointable'
// 			});
			
// 				var joinField = new Ext.form.ComboBox({
// 				fieldLabel:'Join Field',
// 				width:200,
// 				id:'joinfield'
// 			});
		var buttons = new Ext.FormPanel({
			title:'查询面板',
			frame : true,
			region : 'center',
			margins : '5 0 5 0',
			width : 700,
			tbar:[new Ext.Button({
				text:'查询',
				width:150,
				handler:function(){
					var choosetable = Ext.getCmp("choosetable").getValue();
					var choosefeild= Ext.getCmp("choosefeild").getValue();
					var query_pattern= Ext.getCmp("queryPattern").getValue();
					var jointable= Ext.getCmp("jointable").getValue();
					var join_pattern= Ext.getCmp("joinpattern").getValue();
// 					var joinfield= Ext.getCmp("joinfield").getValue();
	var selectcondition= Ext.getCmp("selectcondition").getValue();
					var condition= Ext.getCmp("condition").getValue();
// 					alert(choosetable+choosefeild+query_pattern+jointable+join_pattern+joinfield+condition);
// alert(condition);
					var requestConfig = {
						url:'commonquery.svt?kind=commonquery',
						params:{
							 choosetable:choosetable,
					 choosefield:choosefeild,
					 query_pattern:query_pattern,
					 jointable:jointable,
					 join_pattern:join_pattern,
// 					 joinfield:joinfield,
selectcondition:selectcondition,
					 condition:condition,
					username: a_username,
					url:a_url,
					password:a_password ,
					driver:a_driver
						},
						callback:function(option,success,response){
							var msg = [success,option.customer,response.responseText];
// 							alert(msg);
					//grid.getStore().reload();
						}
					}
					 Ext.Ajax.request(requestConfig);
				}
			}),{
				xtype : 'tbseparator'
			}
			/*,
			new Ext.Button({
				text:'Sematic verify',
				width:100
			})*/
			,{
				xtype : 'tbseparator'
			},new Ext.Button({
				text:'重置',
				width:150,
				handler:function(){
				Ext.getCmp("choosetable").setValue("");
					Ext.getCmp("choosefeild").setValue("");
					Ext.getCmp("queryPattern").setValue("");
					Ext.getCmp("jointable").setValue("");
					Ext.getCmp("joinpattern").setValue("");
				Ext.getCmp("selectcondition").setValue("");
					Ext.getCmp("condition").setValue("");
				}
			})],
			items : [ {
				fieldLabel : '主表',
				xtype : 'textfield',
				name : 'choosetable',
				width:200,
				id:'choosetable'
			} ,
			queryPattern,
			{
				fieldLabel:'条件字段',
				xtype:'textfield'
				,id:'choosefeild',
				width:200
			},{
				fieldLabel:'查询条件',
				xtype:'textfield',
				id:'selectcondition',
				width:200
			},
			joinPattern,
			{
				fieldLabel:'链接表',
				xtype:'textfield'
				,id:'jointable',
				width:200
			}
			,
// 			{
// 				fieldLabel:'Join Field',
// 				xtype:'textfield'
// 				,id:'joinfield',
// 				width:200
// 			},
			{
				fieldLabel:'链接条件',
				xtype:'textarea'
				,id:'condition',
				width:200,
				text:'condition',
				height:40
			}]
		});

		
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////console//////////////////////

		var proxy_5 = new Ext.data.HttpProxy({
			url : 'commonquery.svt?kind=console'
		});

		var reader_5 = new Ext.data.JsonReader({}, [{
			name : 'submitetime'
		}, {
			name : 'hql'
		}, {
			name : 'progress'
		}, {
			name : 'hidden'
		}]);

		var store_5= new Ext.data.Store({
			proxy : proxy_5,
			reader : reader_5
		});

		store_5.load();
		
		var console = new Ext.grid.GridPanel({
			region : 'east',
			store:store_5,
			frame : true,
			margins : '5 5 5 5',
			width : 390,
			title : '任务监控',
			tbar:[new Ext.Button({
				text:'刷新',
				handler:function(){
					
						var requestConfig = {
						url:'commonquery.svt?kind=refresh',
						params:{},
						callback:function(option,success,response){
						}
					}
					 Ext.Ajax.request(requestConfig);
					
				}
			})],
			columns:[{
							id : 'Submit Time',
							header : '任务ID',
							//width : 90,
							sortable : true,
							dataIndex : 'submitetime',
							align : 'center'
						},{
							id : 'hql',
							header : 'HQL查询语句',
							//width : 90,
							sortable : true,
							dataIndex : 'hql',
							width:300,
							align : 'center'
						},{
							id : 'Progress',
							header : 'progress',
							//width : 90,
							sortable : true,
							dataIndex : 'progress',
							align : 'center'
						}
						,{
							id : 'hidden',
							header : '其他信息',
							//width : 90,
							sortable : true,
							dataIndex : 'hidden',
							align : 'center'
						}]
		});

		// 		var container_2 = new Ext.Panel({
		// 			region : 'east',
		// 			layout : 'border',
		// 			frame : false,
		// 			margins : '0 0 0 0',
		// 			width : 500,
		// 			items : [buttons, console]
		// 		});
		
		
			
	self.setInterval(function(){
		console.getStore().reload();
	},1000);
	
	
	console.addListener('cellclick', consoleClick);
	
	function consoleClick(grid, rowIndex, columnIndex, e){
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					var taskId  = selectRows[0].get("hidden");
// 					alert(taskId);
						var requestConfig = {
						url:'commonquery.svt?kind=result',
						params:{taskId:taskId},
						callback:function(option,success,response){
							var msg = [success,option.customer,response.responseText];
// 							alert(msg);
							Ext.getDom("innerPanel").innerHTML = response.responseText;
						}
					}
					 Ext.Ajax.request(requestConfig);
					
	}
	
	
		
////////////////////////////////////////////////////////////////////////////////////////////   console + buttons + tables   /////////////////////////////
		var container_1 = new Ext.Panel({
			layout : 'border',
			region : 'north',
			height : 300,
			margins : '5 5 0 5',
			items : [tables, buttons, console]
		});

		var details = new Ext.Panel({
			region : 'center',
			frame : true,
			margins : '5 5 5 5',
			title : '结果集',
			id:'resultdetail',
			tbar:[
				new Ext.Button({
					text:'导出文本结果',
					handler:function(){
						Ext.MessageBox.prompt("Prompt","请输入导出目标路径：",function(button,text){
							if(text == ""){
								Ext.MessageBox.alert("Prompt","目标路径无效");
								return;
							}
							var content = Ext.getDom("innerPanel").innerHTML;
								var requestConfig = {
						url:'commonquery.svt?kind=exportresult',
						params:{resultset:content,dir:text},
						callback:function(option,success,response){
							var msg = [success,option.customer,response.responseText];
							if(response.responseText=="1"){
							Ext.MessageBox.alert("Prompt","成功");
							}else{
							Ext.MessageBox.alert("Prompt","失败");}
						}
					}
					 Ext.Ajax.request(requestConfig);
						});
					}
				})
			],
			items:[new Ext.form.TextArea({
				id:'innerPanel',
				width:1150,
				height:300,
				bodyStyle :'overflow-x:visible;overflow-y:scroll' 
			})]
		});

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [details, container_1]
		});
		
		
		
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////window
			tables.addListener('cellclick', cellclick);

				function cellclick(grid, rowIndex, columnIndex, e) {
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					a_username = selectRows[0].get("username");
					a_url = selectRows[0].get("url");
					a_password = selectRows[0].get("password");
					a_driver = selectRows[0].get("driver");
					a_tablename  = selectRows[0].get("tablename")

					/***********************************************
							  Window Panel
					 ***********************************************/

					var proxy_1 = new Ext.data.HttpProxy({
						url : 'datamigration.svt?kind=column&username='
								+ a_username + '&url=' + a_url + '&password='
								+ a_password + '&driver=' + a_driver
								+ '&schemaname=' +a_schemaname + '&tablename='
								+ a_tablename
					});

					var reader_1 = new Ext.data.JsonReader({}, [{
						name : 'columnname'
					}, {
						name : 'pk'
					}, {
						name : 'fk'
					}, {
						name : 'referenceto'
					}]);

					var store_1 = new Ext.data.Store({
						proxy : proxy_1,
						reader : reader_1
					});

					store_1.load();

					var win_1 = new Ext.grid.GridPanel({
						store : store_1,
						margins : '5 5 5 0',
						title : 'Columns',
						frame : true,
						width : 300,
						region : 'east',
						columns : [{
							id : 'Column Name',
							header : 'columnname',
							//width : 90,
							sortable : true,
							dataIndex : 'columnname',
							align : 'center'
						}, {
							id : 'Primary Key',
							header : 'pk',
							//width : 60,
							sortable : true,
							dataIndex : 'pk',
							align : 'center'
						}, {
							id : 'Foreign Key',
							header : 'fk',
							//width : 60,
							sortable : true,
							dataIndex : 'fk',
							align : 'center'
						}
						/* 						, {
						 // 							id : 'Reference To',
						 // 							header : 'referenceto',
						 width : 90,
						 // 							sortable : true,
						 // 							dataIndex : 'referenceto',
						 // 							align : 'center'
						 // 						}*/
						]
					});

					var proxy_2 = new Ext.data.HttpProxy({
						url : 'datamigration.svt?kind=table&username='
								+ a_username + '&url=' + a_url + '&password='
								+ a_password + '&driver=' + a_driver
					});

					var reader_2 = new Ext.data.JsonReader({}, [{
						name : 'schemaname'
					}, {
						name : 'tablename'
					}, {
						name : 'rowcount'
					}, {
						name : 'columncount'
					}]);

					var store_2 = new Ext.data.Store({
						proxy : proxy_2,
						reader : reader_2
					});

					store_2.load();

					var win_2 = new Ext.grid.GridPanel({
						store : store_2,
						region : 'center',
						title : 'Tables',
						frame : true,
						margins : '5 5 5 5',
						columns : [{
							id : 'schemaname',
							header : 'schemaname',
							//width : 90,
							sortable : true,
							dataIndex : 'schemaname',
							align : 'center'
						}, {
							id : 'Table Name',
							header : 'tablename',
							//width : 35,
							sortable : true,
							dataIndex : 'tablename',
							align : 'center'
						}, {
							id : 'Row count',
							header : 'rowcount',
							//width : 35,
							sortable : true,
							dataIndex : 'rowcount',
							align : 'center'
						}, {
							id : 'columncount',
							header : 'columncount',
							//width : 35,
							sortable : true,
							dataIndex : 'columncount',
							align : 'center'
						}]
					});

					/////////////////////////////////////////////////////////////////////////////////
					//				win_2(tables) listener
					/////////////////////////////////////////////////////////////////////////////////

					win_2.addListener('cellclick', win_2_cellclick);
					
					var t_schemaname;


					function win_2_cellclick(grid, rowIndex, columnIndex, e) {
						var record = grid.getStore().getAt(rowIndex); //Get the Record   
						var selectRows = grid.getSelectionModel()
								.getSelections();
						a_tablename = selectRows[0].get("tablename");
						var t_schemaname=selectRows[0].get("schemaname");
						win_1.getStore().proxy.conn.url = 'datamigration.svt?kind=column&username='
								+ a_username
								+ '&url='
								+ a_url
								+ '&password='
								+ a_password
								+ '&driver='
								+ a_driver
								+ '&tablename='
								+ a_tablename
								+'&schemaname='
								+t_schemaname;
						win_1.getStore().reload();
					}

					// 			alert(a_username + a_url + a_password + a_driver);

					window = new Ext.Window({
						id : 'window',
						title : 'Details',
						plain : true,
						closable : true,
						layout : 'border',
						height : 350,
						width : 650,
						items : [win_1, win_2]
					});

					// 			window.add(winPanel);
					//window.show();

				}
		
		
		
		
	});
</script>
</head>

<body>
</body>
</html>
